/**
 * Created on 14 avr. 08 by Sebastian Audet
 */
package gui2Dengine;

import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;

/**
 * A triangle class to define the basics of a triangle, as well as some basic
 * functions <br>
 * A Triangle is made up of:
 * <ul>
 * <li>Three Points</li>
 * <li>A center Point</li>
 * <li>A method to return the triangle as a GeneralPath for rendering</li>
 * <li>A static method to return bisector length of a triangle</li>
 * </ul>
 * 
 * @author Sebastian Audet
 */
public class EquilateralTriangle {

    private static final double SIXTY_DEG = Math.PI / 3;
    private static final double THIRTY_DEG = SIXTY_DEG/2;
    private static final double ONE_HUNDRED_TWENTY_DEG = SIXTY_DEG*2;
    private static double defaultlength = 25;
    private double length = defaultlength;
    private Point2D.Double p1 = null;
    private Point2D.Double p2 = null;
    private Point2D.Double p3 = null;
    private Point2D.Double center = null;
    private GeneralPath equilateraltriangle;
    
    public GeneralPath getTriangle() {
	return equilateraltriangle;
    }
    
    private static Point2D.Double getRadialPoint(Point2D.Double p,
	    double length, double degree) {
	return new Point2D.Double((p.getX() + (length * Math.cos(degree))), (p
		.getY() + (length * Math.sin(degree))));
    }

    public static double getBisectorLength(double length) {
	return length * Math.sin(SIXTY_DEG);
    }

    public double getBisectorLength() {
	return length * Math.sin(SIXTY_DEG);
    }

    public Point2D.Double getCenter() {
	return center;
    }
    
    /**
     * Returns the Center of an Equilateral Triangle, given three things:
     * <ul>
     * <li>A corner of the triangle</li>
     * <li>The length of one side</li>
     * <li>The theta angle to turn to face the center of the triangle</li>
     * </ul>
     * @param origpoint The Corner point from which to calculate
     * @param sidelength The length of one side of the triangle
     * @param theta The angle theta to the Center
     * @return
     */
    private Point2D.Double getCenter(Point2D.Double origpoint, double sidelength, double theta) {
	return getRadialPoint(origpoint,getDistanceToCenter(sidelength),theta);
    }

    private double getDistanceToCenter(double sidelength) {
	return (sidelength/2)*Math.cos(THIRTY_DEG);
    }
    
    public double getDistanceToCenter() {
	return getDistanceToCenter(length);
    }

    public EquilateralTriangle(Point2D.Double p,boolean orientation)
    {
	this(p,defaultlength,orientation);
    }
    
    public EquilateralTriangle(Point2D.Double p,double length,boolean orientation)
    {
	this(p,length,0,orientation);
    }
    
    public EquilateralTriangle(Point2D.Double p,double length, double rotationaloffset,boolean orientation)
    {
	this.length=length;
	p1=p;
	//DOWN
	if(!orientation) {
	    center=getCenter(p, length, THIRTY_DEG+rotationaloffset);
	    p2 = getRadialPoint(p, length, SIXTY_DEG+rotationaloffset);
	    p3 = getRadialPoint(p2, length, -SIXTY_DEG+rotationaloffset);
	} else {
	    center=getCenter(p, length,-THIRTY_DEG+rotationaloffset);
	    p2 = getRadialPoint(p, length, rotationaloffset);
	    p3 = getRadialPoint(p2, length, -ONE_HUNDRED_TWENTY_DEG+rotationaloffset);
	}
	equilateraltriangle = new GeneralPath();
	equilateraltriangle.moveTo(p1.x, p1.y);
	equilateraltriangle.lineTo(p2.x, p2.y);
	equilateraltriangle.lineTo(p3.x, p3.y);
	equilateraltriangle.closePath();
    }

    public double getSideLength() {
	return length;
    }

    /*public static void main(String[] args) {
	JFrame frame = new JFrame() {
	    /**
	     * long serialVersionUID
	     */
	    /*private static final long serialVersionUID = 1L;

	    public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setBackground(Color.red);
		g2.setColor(Color.green);
		g2.setPaint(Color.blue);
		g2.setStroke(new BasicStroke(0.2f));
		GeneralPath path = new EquilateralTriangle(new Point2D.Double(0,40),50,true).getTriangle();
		GeneralPath path2 = new EquilateralTriangle(new Point2D.Double(0,40),50,false).getTriangle();
		g2.draw(path);
		//g2.draw(path2);
		}
	};
	frame.setSize(800, 800);
	frame.setVisible(true);
}*/
}
